﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.Data.Entity;

namespace DNA.Mvc.Community.Data.Entity
{
    public class ForumContext : UnitOfWorkBase<ForumDb>, IForumContext
    {
        public ForumContext(string nameOrConnectionString)
        {
            this.dbContext = new ForumDb(nameOrConnectionString);
        }

        public ForumContext()
        {
            this.dbContext = new ForumDb();
        }

        private IForumRepository forumRepository;
        private IPostRepostiory postRepostiory;
        private IThreadRepository threadRepository;
        private IRepository<Moderator> moderators;
        private IRepository<Attachment> attachments;
        private IRepository<Rank> ranks;

        public IForumRepository Forums
        {
            get
            {
                if (forumRepository == null)
                    forumRepository = new ForumRepository(this.dbContext);
                return forumRepository;
            }
        }

        public IPostRepostiory Posts
        {
            get
            {
                if (postRepostiory == null)
                    postRepostiory = new PostRepository(this.dbContext);
                return postRepostiory;
            }
        }

        public IThreadRepository Threads
        {
            get
            {
                if (threadRepository == null)
                    threadRepository = new ThreadRepository(this.dbContext);
                return threadRepository;
            }
        }

        public Thread CreateThread(int forumID, Post post, bool isPinned, bool isLocked)
        {

            var forum = Forums.Find(forumID);
            var thread = new Thread()
            {
                Forum = forum,
                IsPinned = isPinned,
                IsLocked = isLocked,
                Posted = DateTime.Now,
                Title = post.Title,
                Author = post.UserName,
                IsApproved =post.IsApproved,
                Approved=post.Approved,
                LastPosted=DateTime.Now,
                TotalPosts=0,
                LastPostUser =post.UserName
            };

            Threads.Create(thread);
            post.Thread = thread;
            post.IsThread = true;
            post.Posted = DateTime.Now;
            post.LastModified = DateTime.Now;
            post.IsLocked = isLocked;
            post.ForumID = forumID;

            Posts.Create(post);
            SaveChanges();

            post.ParentID = post.ID;

            if (post.IsApproved)
            {
                thread.LastPostID = post.ID;
                thread.Forum.LastPostID = post.ID;
            }

            SaveChanges();

            return thread;
        }

//        protected override EntityRepositoryBase<ForumDb, T> GenericRepository<T>()
   //     {
      //      return new GenericForumRepository<T>(this.dbContext);
      //  }

        public IRepository<Moderator> Moderators
        {
            get
            {
                if (moderators == null)
                    moderators = GenericRepository<Moderator>();
                return moderators;
            }
        }

        public IRepository<Rank> Ranks
        {
            get
            {
                if (ranks == null)
                    ranks = GenericRepository<Rank>();
                return ranks;
            }
        }

        public IRepository<Attachment> Attachments
        {
            get 
            {
                if (attachments == null)
                    attachments = GenericRepository<Attachment>();
                return attachments;
            }
        }
    }
}
